package com.jxs.tourist.controller;

import com.jxs.tourist.domain.User;
import com.jxs.tourist.domain.UserInfo;
import com.jxs.tourist.domain.UserListBean;
import com.jxs.tourist.service.UserInfoService;
import com.jxs.tourist.service.UserService;
import com.jxs.tourist.util.FileUpload;
import com.jxs.tourist.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Controller
@RequestMapping("/user")
public class UserController {
    public UserController() {
        System.out.println("UserController");
    }

    @Resource
    private UserService userService;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 管理员后台登陆
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes attr) {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        System.out.println("dsfasdf   "+account);
        User user = userService.selectByPrimaryKey(account);
        if (user != null) {
            if (password.equals(user.getPassword()) && user.getActype() == 0) {
                attr.addAttribute("account", user.getPhone());
                Cookie cookie = new Cookie("loginUser", user.getId() + "");
                response.addCookie(cookie);
                return "redirect:/admin/index.jsp";
            }
            model.addAttribute("msg", "密码不正确");
            return "error";
        }
        model.addAttribute("msg", "用户不存在");
        return "error";
    }

    /**
     * 管理查看管理员
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showAdmin", method = RequestMethod.POST)
    public JsonResult<List<UserListBean>> showAdmin(HttpServletRequest request) {
        List<UserListBean> list = userService.getAllUser(0);
        JsonResult<List<UserListBean>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<UserListBean>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<UserListBean>>(list, "获取成功", 0);
        }
        return result;
    }

    /**
     * 管理查看所有用户
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showUser", method = RequestMethod.POST)
    public JsonResult<List<UserListBean>> allUser(HttpServletRequest request) {
        List<UserListBean> list = userService.getAllUser(1);
        JsonResult<List<UserListBean>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<UserListBean>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<UserListBean>>(list, "获取成功", 0);
        }
        return result;
    }

    /**
     * 删除管理员
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteAdmin", method = RequestMethod.POST)
    public JsonResult<String> deleteAdmin(HttpServletRequest request) {
        JsonResult<String> result;
        if (userService.deleteAdmin(Integer.valueOf(request.getParameter("user_id"))) == 1) {
            result = new JsonResult<String>("", "删除成功", 0);
        } else {
            result = new JsonResult<String>("", "删除失败", 1);
        }
        return result;
    }
//    /**
//     * 查找所有用户分页
//     * @param request
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/showUserByPage",method = RequestMethod.POST)
//    public JsonResult<List<User>> allUserByPage(HttpServletRequest request) {
//        List<User> list = userService.getAllUserByPage((Integer.valueOf(request.getParameter("page"))-1)*Integer.valueOf(request.getParameter("pageSize")),Integer.valueOf(request.getParameter("pageSize")));
//        JsonResult<List<User>> result;
//        if (list == null || list.size() == 0) {
//            result = new JsonResult<List<User>>(list, "暂时没有数据", 1);
//        } else {
//            result = new JsonResult<List<User>>(list, "获取成功", 0);
//        }
//        return result;
//    }

    /**
     * 普通用户登陆
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    public JsonResult<User> userLogin(HttpServletRequest request) {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        User user = userService.selectByPrimaryKey(account);
        if (user != null) {
            if (password.equals(user.getPassword())) {
                return new JsonResult<User>(user, "登陆成功", 0);
            } else {
                return new JsonResult<User>(user, "密码不正确", 1);
            }
        }
        return new JsonResult<User>(user, "账号不存在", 1);
    }

    /**
     * 新增管理员
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
    public JsonResult<String> addAdmin(HttpServletRequest request) {
        System.out.println(request.getParameterMap().toString());
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        User user = userService.selectByPrimaryKey(account);
        if (user == null) {
            user = new User();
            user.setPhone(account);
            user.setActype(0);
            user.setPassword(password);
            user.setCreatetime(new Date());
            System.out.println(account+" "+password);
            if (userService.insert(user) == 1) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUser_id(userService.selectByPrimaryKey(account).getId());
                userInfo.setAvatar("");
                userInfo.setNickname("");
                userInfoService.insertUserInfo(userInfo);
                return new JsonResult<String>("", "添加成功", 0);
            } else {
                return new JsonResult<String>("", "添加失败", 1);
            }
        }
        return new JsonResult<String>("", "管理员已存在", 1);
    }

    /**
     * 普通用户注册
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userRegister", method = RequestMethod.POST)
    public JsonResult<String> userRegister(HttpServletRequest request) {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        User user = userService.selectByPrimaryKey(account);
        if (user == null) {
            user = new User();
            user.setPhone(account);
            user.setActype(1);
            user.setPassword(password);
            user.setCreatetime(new Date());
            if (userService.insert(user) == 1) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUser_id(userService.selectByPrimaryKey(account).getId());
                userInfo.setAvatar("");
                userInfo.setNickname("");
                userInfoService.insertUserInfo(userInfo);
                return new JsonResult<String>("", "注册成功", 0);
            } else {
                return new JsonResult<String>("", "注册失败", 1);
            }
        }
        return new JsonResult<String>("", "用户已存在", 1);
    }

    /**
     * 修改密码
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userModifyPwd", method = RequestMethod.POST)
    public JsonResult<String> userModifyPwd(HttpServletRequest request) {
        String userId = request.getParameter("user_id");
        String password = request.getParameter("password");
        User user = userService.selectByUserId(Integer.valueOf(userId));
        if (user != null) {
            user.setPassword(password);
            if (userService.editPassword(user) == 1) {
                return new JsonResult<String>("", "修改成功", 0);
            } else {
                return new JsonResult<String>("", "修改失败", 1);
            }
        }
        return new JsonResult<String>("", "用户不存在", 1);
    }

    /**
     * 用户修改信息
     *
     * @param request
     * @param model
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/changeUserInfo")
    public JsonResult<String> changeUserInfo(HttpServletRequest request, Model model){
        User user = userService.selectByUserId(Integer.valueOf(request.getParameter("user_id")));
        if (user != null) {
            String nickname = request.getParameter("nickname");
            UserInfo info = userInfoService.getUserInfoById(user.getId());
            info.setNickname(nickname);
            info.setAvatar(request.getParameter("avatar"));
            if (userInfoService.editUserInfo(info) == 1) {
                return new JsonResult<String>("", "修改成功", 0);
            } else {
                return new JsonResult<String>("", "修改失败", 1);
            }
        }
        return new JsonResult<String>("", "用户不存在", 1);
    }
    /**
     * 用户修改信息
     *
     * @param request
     * @param model
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult<String> uploadImg(@RequestParam(value = "file",required = true) MultipartFile file, HttpServletRequest request, Model model){
        try {
            String path= FileUpload.uploadFile(file,request);
            return new JsonResult<String>(path, "上传成功", 0);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonResult<String>("", "上传失败", 1);
    }
    /**
     * 用户修改信息
     * @param request
     * @param model
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/getUserInfo")
    public JsonResult<UserInfo> getUserInfo(HttpServletRequest request, Model model) throws IOException {
        UserInfo user = userInfoService.getUserInfoById(Integer.valueOf(request.getParameter("user_id")));
        if (user != null) {
            return new JsonResult<UserInfo>(user, "获取成功", 0);
        }
        return new JsonResult<UserInfo>(user, "用户不存在", 1);
    }
}
